package limeirafol.gui;

import lbframework.bean.dialog.LBDialog;
import lbframework.bean.mdiFrame.LBMdiFrame;
import lbframework.classe.Util;
import limeirafol.Global;
import limeirafol.bean.BeanCadastro;
import limeirafol.remote.UsuarioVO;

public class LoginGUI extends LBDialog {

    private BeanCadastro beanCadastro = new BeanCadastro();
    private UsuarioVO oUsuario = null;
    private LBMdiFrame mdiFrame = null;

    public LoginGUI(LBMdiFrame i_mdiFrame) throws Exception {
        initComponents();
        centralizarForm();
        mdiFrame = i_mdiFrame;
        this.setModal(true);

        lblVersao.setText("Versão " + Global.VERSAO);
    }

    public void autenticar() throws Exception {

        oUsuario = beanCadastro.autenticarUsuario(txtUsuario.getText(), txtSenha.getText());

        Global.idUsuario = oUsuario.id;
        Global.usuario = oUsuario.nome;

        Global.verificarVersao();

        MenuGUI form = new MenuGUI(this);

        form.atualizarRodape();
        form.setVisible(true);

        if (mdiFrame != null) {
            mdiFrame.dispose();
        }

        mdiFrame = form;

        this.dispose();

        form.requestFocus();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lBPanel1 = new lbframework.bean.panel.LBPanel();
        txtUsuario = new lbframework.bean.textField.LBTextField();
        lBLabel2 = new lbframework.bean.label.LBLabel();
        lBLabel3 = new lbframework.bean.label.LBLabel();
        txtSenha = new lbframework.bean.passwordField.LBPasswordField();
        lblPrograma = new lbframework.bean.label.LBLabel();
        lBPanel2 = new lbframework.bean.panel.LBPanel();
        lBButton2 = new lbframework.bean.button.LBButton();
        lblVersao = new lbframework.bean.label.LBLabel();
        lBButton3 = new lbframework.bean.button.LBButton();
        lBLabel1 = new lbframework.bean.label.LBLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Limeira Folheados");
        setIconImage(null);
        setUndecorated(true);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lBPanel1.setBorder(null);

        lBLabel2.setText("Usuário");

        lBLabel3.setText("Senha");

        javax.swing.GroupLayout lBPanel1Layout = new javax.swing.GroupLayout(lBPanel1);
        lBPanel1.setLayout(lBPanel1Layout);
        lBPanel1Layout.setHorizontalGroup(
            lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, lBPanel1Layout.createSequentialGroup()
                .addGap(93, 93, 93)
                .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtSenha, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, lBPanel1Layout.createSequentialGroup()
                        .addComponent(lBLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, lBPanel1Layout.createSequentialGroup()
                        .addComponent(lBLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 89, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(28, 28, 28))
        );
        lBPanel1Layout.setVerticalGroup(
            lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lBPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lBLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lBLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        getContentPane().add(lBPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 70, 230, 120));

        lblPrograma.setForeground(new java.awt.Color(51, 51, 51));
        lblPrograma.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblPrograma.setText("Limeira Folheados");
        lblPrograma.setFont(new java.awt.Font("Tahoma", 1, 18));
        getContentPane().add(lblPrograma, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 20, 200, 30));

        lBPanel2.setBorder(null);

        lBButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/autenticar.png"))); // NOI18N
        lBButton2.setText("Autenticar");
        lBButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lBButton2ActionPerformed(evt);
            }
        });

        lblVersao.setText("X.X.X");

        lBButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/sair.png"))); // NOI18N
        lBButton3.setText("Sair");
        lBButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lBButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout lBPanel2Layout = new javax.swing.GroupLayout(lBPanel2);
        lBPanel2.setLayout(lBPanel2Layout);
        lBPanel2Layout.setHorizontalGroup(
            lBPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, lBPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblVersao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 164, Short.MAX_VALUE)
                .addComponent(lBButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lBButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        lBPanel2Layout.setVerticalGroup(
            lBPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lBPanel2Layout.createSequentialGroup()
                .addGroup(lBPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(lBPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lBButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lBButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(lBPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblVersao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(lBPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 230, 390, 30));

        lBLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lBLabel1.setPreferredSize(new java.awt.Dimension(399, 286));
        getContentPane().add(lBLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 410, 270));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lBButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lBButton2ActionPerformed

        try {
            autenticar();
        } catch (Exception e) {
            Util.exibirMensagemErro(e, getTitle());
        }

    }//GEN-LAST:event_lBButton2ActionPerformed

    private void lBButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lBButton3ActionPerformed

        try {
            sair();
        } catch (Exception e) {
        }

    }//GEN-LAST:event_lBButton3ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.button.LBButton lBButton2;
    private lbframework.bean.button.LBButton lBButton3;
    private lbframework.bean.label.LBLabel lBLabel1;
    private lbframework.bean.label.LBLabel lBLabel2;
    private lbframework.bean.label.LBLabel lBLabel3;
    private lbframework.bean.panel.LBPanel lBPanel1;
    private lbframework.bean.panel.LBPanel lBPanel2;
    private lbframework.bean.label.LBLabel lblPrograma;
    private lbframework.bean.label.LBLabel lblVersao;
    private lbframework.bean.passwordField.LBPasswordField txtSenha;
    private lbframework.bean.textField.LBTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
